package com.yzjs.localhost.service.hrmAwardSanction.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dao.hrmAwardSanction.HrmAwardSanctionMapper;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmAwardSanction.HrmAwardSanctionEntity;
import com.yzjs.localhost.entity.sysDept.SysDeptEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmAwardSanction.HrmAwardSanctionService;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


@Service("hrmAwardSanctionService")
public class HrmAwardSanctionServiceImpl extends BaseServiceImpl<HrmAwardSanctionEntity, HrmAwardSanctionMapper> implements HrmAwardSanctionService {

    @Autowired
    AnalysisService analysisService;

    @Override
    public ComparisonResultDTO AwardSanctionCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity before = dto.getBeforce();
        String beforeInfo = before.getInfo();
        Map<String, Object> beforeParse = JsonUtil.json2map(beforeInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        Map<String, Object> afterParseMap = JsonUtil.json2map(after.getInfo());
        Map<String, Object> differenceMap = new HashMap<>();
        ComparisonResultDTO resultDTO = null;

        if (before.getOperatorCode().contains("sanction:add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 奖罚文件号,奖罚金额,奖罚内容,奖罚单位,奖罚原因,奖罚日期,操作人,用户ID,奖罚类型
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "fileNumber,hasMoney,hasContent,hasCompany,hasReason,hasDate,createBy,hasUserId,hasType");

        } else if (before.getOperatorCode().contains("sanction:edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 对比 奖罚文件号,奖罚金额,奖罚内容,奖罚单位,奖罚原因,奖罚日期,删除标志,操作人,主键ID,更新人,用户ID,奖罚类型,操作时间
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "fileNumber,hasMoney,hasContent,hasCompany,hasReason,hasDate,deleteFlag,createBy,hasId,updateBy,hasUserId,hasType,createDate");

        } else if (before.getOperatorCode().contains("sanction:remove")) {
            beforeParse.put("deleteFlag", "2");
            // 比对人奖罚记录主键ID
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParseMap, "hasIds,deleteFlag");
            Object hasId = afterParseMap.get("hasIds");
            if (hasId == null) {
                resultDTO.setAbstractName("主键ID不存在");
                resultDTO.setWarningInfo("主键ID不存在");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            HrmAwardSanctionEntity awardSanctionEntity = new HrmAwardSanctionEntity();
            awardSanctionEntity.setHasId(Long.valueOf((String) hasId));
            awardSanctionEntity.setDeleteFlag((String) afterParseMap.get("deleteFlag"));
            awardSanctionEntity.setDataIdentification(after.getDataIdentification());
            this.insert(awardSanctionEntity);
            return resultDTO;
        }
        // 记录表信息
        HrmAwardSanctionEntity hrmAwardSanctionEntity = JSONObject.parseObject(afterInfo, HrmAwardSanctionEntity.class);
        hrmAwardSanctionEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmAwardSanctionEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmAwardSanctionEntity);
        return resultDTO;
    }
}